/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.poc.base;

import com.herenit.beijing.dh2011.security.entity.SecurityMenu;
import com.herenit.beijing.poc.base.node.MenuChildrenFactory;
import com.herenit.beijing.poc.base.node.MenuNode;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.actions.CopyAction;
import org.openide.actions.CutAction;
import org.openide.actions.DeleteAction;
import org.openide.actions.PasteAction;
import org.openide.awt.ActionID;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.explorer.view.NodeTableModel;
import org.openide.nodes.Children;
import org.openide.util.NbBundle;
import org.openide.util.actions.SystemAction;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.herenit.beijing.poc.base//ModuleList//EN",
autostore = false)
@TopComponent.Description(preferredID = "ModuleListTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "System", id = "com.herenit.beijing.poc.base.ModuleListTopComponent")
@TopComponent.OpenActionRegistration(displayName = "#CTL_ModulelistAction",
preferredID = "ModuleListTopComponent")
public final class ModuleListTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static final String ROOT_NODE = "Explorer";
    private final ExplorerManager manager = new ExplorerManager();
    private List<SecurityMenu> securityMenus;
    private SecurityMenu securityMenu;
    private NodeTableModel ntm;

    public ModuleListTopComponent() {
        initData();  
        initComponents();
        initTree();
        initActions();
        associateLookup(ExplorerUtils.createLookup(manager, getActionMap()));
        setName(NbBundle.getMessage(ModuleListTopComponent.class, "CTL_ModuleListTopComponent"));
        setToolTipText(NbBundle.getMessage(ModuleListTopComponent.class, "HINT_ModuleListTopComponent"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return manager;
    }

    private void initData() {
        securityMenus = new ArrayList<SecurityMenu>();

        SecurityMenu securityMenu0 = new SecurityMenu();
        securityMenu0.setMenuId("0");
        securityMenu0.setDisplayName(
                "根节点");
        securityMenu0.setIcon(
                "com/galileo/netbeans/module/explorer.png");
        securityMenu0.setPosition(
                100);
        securityMenus.add(securityMenu0);
        securityMenu = securityMenu0;

        SecurityMenu securityMenu1 = new SecurityMenu();
        securityMenu1.setMenuId("1");
        securityMenu1.setDisplayName(
                "mp3播放");
        securityMenu1.setIcon(
                "com/galileo/netbeans/module/player.png");
        securityMenu1.setPosition(
                200);
        securityMenus.add(securityMenu1);
        securityMenu1.setFatherId(securityMenu0);

        SecurityMenu securityMenu2 = new SecurityMenu();
        securityMenu2.setMenuId("2");
        securityMenu2.setMenuPath(
                "");

        securityMenu2.setDisplayName(
                "播放列表");
        securityMenu2.setOriginalFile(
                "Actions/Edit/com-galileo-netbeans-module-PlaylistAction.instance");
        securityMenu2.setPosition(
                300);
        securityMenu2.setLeaf(true);
        securityMenus.add(securityMenu2);
        securityMenu2.setFatherId(securityMenu1);


        SecurityMenu securityMenu3 = new SecurityMenu();
        securityMenu3.setMenuId("3");
        securityMenu3.setDisplayName(
                "查看");
        securityMenu3.setIcon(
                "com/galileo/netbeans/module/views.png");
        securityMenu3.setPosition(
                400);

        securityMenus.add(securityMenu3);
        securityMenu3.setFatherId(securityMenu0);

        SecurityMenu securityMenu4 = new SecurityMenu();
        securityMenu4.setMenuId("4");
        securityMenu4.setMenuPath(
                "Actions/Window/org-netbeans-core-io-ui-IOWindowAction");
        securityMenu4.setLeaf(true);
        securityMenu4.setDisplayName(
                "输出");
        securityMenu4.setOriginalFile(
                "Actions/Window/org-netbeans-core-io-ui-IOWindowAction.instance");
        securityMenu4.setPosition(
                500);
        securityMenus.add(securityMenu4);
        securityMenu4.setFatherId(securityMenu3);

        SecurityMenu securityMenu5 = new SecurityMenu();
        securityMenu5.setMenuId("5");
        securityMenu5.setDisplayName(
                "收藏夹");
        securityMenu5.setIcon(
                "com/galileo/netbeans/module/favorites.png");
        securityMenu5.setPosition(
                600);
        securityMenus.add(securityMenu5);
        securityMenu5.setFatherId(securityMenu0);

        SecurityMenu securityMenu6 = new SecurityMenu();
        securityMenu6.setMenuId("6");
        securityMenu6.setMenuPath(
                "Actions/Window/org-netbeans-modules-favorites-View.instance");
        securityMenu6.setLeaf(true);
        securityMenu6.setDisplayName(
                "查看收藏夹");
        securityMenu6.setOriginalFile(
                "Actions/Window/org-netbeans-modules-favorites-View.instance");
        securityMenu6.setPosition(
                700);
        securityMenus.add(securityMenu6);
        securityMenu6.setFatherId(securityMenu5);

        SecurityMenu securityMenu7 = new SecurityMenu();
        securityMenu7.setMenuId("7");
        securityMenu7.setDisplayName(
                "患者管理");
        securityMenu7.setPosition(
                800);
        securityMenus.add(securityMenu7);
        securityMenu7.setFatherId(securityMenu0);

        SecurityMenu securityMenu8 = new SecurityMenu();
        securityMenu8.setMenuId("8");
        securityMenu8.setMenuPath(
                "Actions/Window/com.herenit.beijing.poc.empi.EmpiListTopComponent.instance");
        securityMenu8.setLeaf(true);
        securityMenu8.setDisplayName(
                "患者主索引查询");
        securityMenu8.setOriginalFile(
                "Actions/Window/com-herenit-beijing-poc-empi-EmpiListTopComponent.instance");
        securityMenu8.setPosition(
                900);
        securityMenus.add(securityMenu8);
        securityMenu8.setFatherId(securityMenu7);

        SecurityMenu securityMenu9 = new SecurityMenu();
        securityMenu9.setMenuId("9");
        securityMenu9.setMenuPath(
                "Actions/Window/com.herenit.beijing.poc.empi.EmpiTopComponent.instance");
        securityMenu9.setLeaf(true);
        securityMenu9.setDisplayName(
                "新建患者主索引");
        securityMenu9.setOriginalFile(
                "Actions/Window/com-herenit-beijing-poc-empi-EmpiTopComponent.instance");
        securityMenu9.setPosition(
                1000);
        securityMenus.add(securityMenu9);
        securityMenu9.setFatherId(securityMenu7);
    }

    private void initTree() {

        BeanTreeView btv = new BeanTreeView();
        MenuNode menuRoot = new MenuNode(securityMenu, Children.create(new MenuChildrenFactory(securityMenus, securityMenu), false));
        manager.setRootContext(menuRoot);
//        ntm = new NodeTableModel();
//        ntm.setNodes(menuRoot.getChildren().getNodes());
//        Node.Property[] props = menuRoot.getPropertySets()[0].getProperties();
//        ntm.setProperties(props);
        //   TableView btv = new TableView(ntm);
        //   manager.setExploredContext(new MenuNode(securityMenu, Children.create(new MenuChildrenFactory(securityMenus, securityMenu), false)));
        this.add(btv, BorderLayout.CENTER);


//        FileObject root = FileUtil.getConfigFile(ROOT_NODE);
//        if (root != null) { /*
//             * folder found
//             */
//            manager.setRootContext(
//                    new ExplorerFolderNode(root, Children.create(
//                    new ExplorerChildFactory(root), false)));
//        }

    }

    private void initActions() {
        CutAction cut = SystemAction.get(CutAction.class);
        getActionMap().put(cut.getActionMapKey(), ExplorerUtils.actionCut(manager));
        CopyAction copy = SystemAction.get(CopyAction.class);
        getActionMap().put(copy.getActionMapKey(), ExplorerUtils.actionCopy(manager));
        PasteAction paste = SystemAction.get(PasteAction.class);
        getActionMap().put(paste.getActionMapKey(), ExplorerUtils.actionPaste(manager));
        DeleteAction delete = SystemAction.get(DeleteAction.class);
        getActionMap().put(delete.getActionMapKey(), ExplorerUtils.actionDelete(manager, true));
    }

    @Override
    protected void componentActivated() {
        ExplorerUtils.activateActions(manager, true);
    }

    @Override
    protected void componentDeactivated() {
        ExplorerUtils.activateActions(manager, false);
    }
}
